from django import http
from django.shortcuts import render

from django.views import View

from contents.utils import get_categories
from goods.models import GoodsCategory
from goods.utils import get_breadcrumb


# Create your views here.
class ListView(View):
    """商品列表页"""
    def get(self, request, category_id, page_num):
        """提供商品列表页"""
        sort = request.GET.get('sort', 'default')

        # 参数校验
        try:
            category = GoodsCategory.objects.get(id=category_id)
        except GoodsCategory.DoesNotExist:
            return http.HttpResponseForbidden('GoodsCategory does not exist')
        categories  = get_categories()
        cat2 = category.parent
        cat1 = cat2.parent
        # 面包屑导航 1-2-3
        breadcrumb = get_breadcrumb(category)
        # breadcrumb = {
        #     'cat1': cat1,
        #     'cat2': cat2,
        #     'cat3': category
        # }
        # 排序


        context = {
            'categories': categories,
            'breadcrumb': breadcrumb,
        }
        return render(request, 'list.html', context)